<?php
class SQL {
    private $connect;

    // MYSQL CONNECTOR
    function __construct() {
        $this->connect=mysql_connect('localhost',MYSQL_USER,MYSQL_PASS) or die('Connect fail !');
        mysql_select_db(MYSQL_DB);
        mysql_query('SET NAMES "utf8"');
    }

    // Close Database
    function __destruct() {
        mysql_close($this->connect);
    }

    // Mysql Fetch Data
    function run($sql) {
        $package=mysql_query($sql);
        if (($package)&&(mysql_num_rows($package)>0)){
            while ($info=mysql_fetch_array($package,MYSQL_ASSOC)) $result[]=$info;
            return $result;
        }
    }

    // Mysql Delete Record
    function del($condition) {
        $i=0;$a='';
        while ($condition[$i]!='_') {
            $a.=$condition[$i++];
        }
        $sql='DELETE FROM '.$a.' WHERE '.$condition;
        $this->run($sql);
    }

    // Mysql Insert Record
    function ins($col,$value) {

    }

    // Mysql Get Data From Database
    function get($table,$condition=' ') {
        $select = 'SELECT * FROM '.$table.' ';
        // Comparative Operations
        $tmp=array();
        for($i=0;$i<=strlen($condition)-1;$i++)
            $tmp[]=$condition[$i];
        $compares = array('<','>','=',':');
        foreach ($compares as $compare) {
            if (in_array($compare,$tmp)) {
                $select .= ' WHERE ';
                break;
            }
        }
        $k = strlen($condition)-1;
        $str = '';
        $arr = array();
        $mark = array();
        $position = array(0);

        for ($i=0;$i<=$k;$i++) {
            if ($condition[$i]=='"')
                $position[]=$i;
        }

        for ($i=1;$i<=count($position)/2;$i+=2) {
            $mark[]=substr($condition,$position[$i],$position[$i+1]-$position[$i]+1);
        }
        $dem=0;
        foreach ($mark as $item) {
            $dem++;
            $condition=str_replace($item,'$'.$dem,$condition);
        }

        for ($i=0;$i<=$k;$i++) {
            $m=ord($condition[$i]);
            if ((($m>=65)&&($m<=90))||(($m>=97)&&($m<=122))) {
                $str.=$condition[$i];
            } else {
                if ((strlen($str)>=1) and (!in_array($str,$arr)))
                    $arr[]=$str;
                $str='';
            }
        }

        foreach ($arr as $key) {
            if (strlen($key)>=1)
                $condition=str_replace($key,$table.'_'.$key,$condition);
        }

        $condition=str_replace('@',' ORDER BY ',$condition);
        $condition=str_replace('+',' ASC ',$condition);
        $condition=str_replace('-',' DESC ',$condition);
        $condition=str_replace('&',' AND ',$condition);
        $condition=str_replace('|',' OR ',$condition);
        $condition=str_replace(':',' LIKE ',$condition);
        $condition=str_replace('[',' LIMIT ',$condition);
        $condition=str_replace(']','',$condition);
        $condition=str_replace('.',' , ',$condition);

        // Restore variable
        $dem=0;
        foreach ($mark as $item) {
            $dem++;
            $condition=str_replace('$'.$dem,$item,$condition);
        }

        $select.=$condition;
		echo $select.'<br/><br/>';
        return $this->run($select);
    }

    // MAKE RELATIONAL SQL
    function rel($aselect,$bselect,$condition='',$other='') {
        $a=''; $b=''; $select=''; $i=0;
        while ($condition[$i]!='_') {
            $a.=$condition[$i++];
        }
        while ($condition[++$i]!='=');
        $i++;
        while ($condition[$i]!='_') {
            $b.=$condition[$i++];
        }
        $condition=str_replace($a.'_',$a.'.'.$a.'_',$condition);
        $condition=str_replace($b.'_',$b.'.'.$b.'_',$condition);
        $as=explode('-',$aselect);
        $bs=explode('-',$bselect);
        foreach ($as as $value) {
            $select.=$a.'.'.$a.'_'.$value.',';
        }
        foreach ($bs as $value) {
            $select.=$b.'.'.$b.'_'.$value.',';
        }
        $select[strlen($select)-1]=' ';
        $sql='SELECT '.$select.' FROM '.$a.' LEFT JOIN '.$b.' ON '.$condition.' '.$other;
        return $this->run($sql);
    }
}